package edu.gzc.nio.datagram;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.Scanner;

/**
 * @Description: 客户端，使用DatagramChannel发给送UDP数据包
 * @Author: guozongchao
 * @Date: 2021/1/8 9:50
 */
public class Client {
    public static void main(String[] args) throws IOException {
        DatagramChannel dgChannel = DatagramChannel.open();

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.next();
            buffer.put(s.getBytes());
            buffer.flip();
            dgChannel.send(buffer, new InetSocketAddress("111.229.195.54", 9898));
            buffer.clear();
        }
        dgChannel.close();
    }
}
